<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编号" prop="commodityId">
        <el-input
          v-model="queryParams.commodityId"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名字" prop="commodityName">
        <el-input
          v-model="queryParams.commodityName"
          placeholder="请输入商品名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品分类" prop="commodityClassify">
        <el-select v-model="queryParams.commodityClassify" placeholder="请选择商品分类" clearable>
          <el-option
            v-for="dict in dict.type.erp_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品品牌" prop="commodityBrand">
        <el-select v-model="queryParams.commodityBrand" placeholder="请选择商品品牌" clearable>
          <el-option
            v-for="dict in dict.type.erp_phone_brand"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="commodityFlag">
        <el-select v-model="queryParams.commodityFlag" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.erp_commodity_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建日期" prop="commodityTime">
        <el-date-picker clearable
          v-model="queryParams.commodityTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
<!-- v-hasPermi="['system:controller:add']" -->
<!-- 新增修改代码 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="selectAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:controller:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:controller:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:controller:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="controllerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品编号" align="center" prop="commodityId" />
      <el-table-column label="商品名字" align="center" prop="commodityName" />
      <el-table-column label="商品分类" align="center" prop="commodityClassify">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_category" :value="scope.row.commodityClassify"/>
        </template>
      </el-table-column>
      <el-table-column label="商品品牌" align="center" prop="commodityBrand">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_phone_brand" :value="scope.row.commodityBrand"/>
          <dict-tag :options="dict.type.erp_food_brand" :value="scope.row.commodityBrand"/>
          <dict-tag :options="dict.type.erp_home_brand" :value="scope.row.commodityBrand"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="commodityFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_commodity_flag" :value="scope.row.commodityFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" prop="commodityTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.commodityTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="selectUpdate(scope.row)"
            v-hasPermi="['system:controller:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:controller:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改商品列表对话框 -->
    <el-dialog :title="title" :visible.sync="spopen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品分类" prop="commodityClassify">
          <el-select v-model="form.commodityClassify" placeholder="请选择商品分类">
            <el-option
              v-for="dict in dict.type.erp_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="selectForm">确 定</el-button>
        <el-button @click="selectCancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改商品列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名字" prop=" ">
          <el-input v-model="form.commodityName" placeholder="请输入商品名字" />
        </el-form-item>
        <el-form-item label="商品分类" prop="commodityClassify">
<!--          v-model="form.commodityClassify"-->
          <el-select v-text="Classify" disabled vlaceholder="请选择商品分类" value="Classify">
            <el-option
              :key="dict.value"
              :label="dict.label"
              :value="Classify"
              hidden
            ></el-option>
          </el-select>
        </el-form-item>
<!--        dict in dict.type.erp_home_brand-->
        <el-form-item label="商品品牌" prop="commodityBrand">
          <el-select v-model="form.commodityBrand" placeholder="请选择商品品牌">
            <el-option v-if="commodity_food == 1"
              v-for="dict in dict.type.erp_food_brand" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
            <el-option v-if="commodity_phone == 1"
              v-for="dict in dict.type.erp_phone_brand" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
            <el-option v-if="commodity_home == 1"
              v-for="dict in dict.type.erp_home_brand" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="commodityFlag">
          <el-select v-model="form.commodityFlag" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.erp_commodity_flag"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建日期" prop="commodityTime">
          <el-date-picker clearable
            v-model="form.commodityTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listController, getController, delController, addController, updateController } from "@/api/system/controller";

export default {
  name: "Controller",
  dicts: ['erp_phone_brand', 'erp_commodity_flag', 'erp_category',
  'erp_food_brand','erp_home_brand'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品列表表格数据
      controllerList: [],
      // 弹出层标题
      title: "",
      // 商品分类属性
      Classify: "null",
      // 商品品牌属性
      commodity: "null",
      // 进口食品属性
      commodity_food: null,
      // 数码产品属性
      commodity_phone: null,
      // 生活家电属性
      commodity_home: null,
      // 是否显示弹出层
      addopen: false,
      // 是否显示弹出层
      open: false,
      // 是否显示商品分类层
      spopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        commodityId: null,
        commodityName: null,
        commodityClassify: null,
        commodityBrand: null,
        commodityFlag: null,
        commodityTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表列表 */
    getList() {
      this.loading = true;
      listController(this.queryParams).then(response => {
        this.controllerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.spopen = false;
      this.Classify = "null";
      this.reset();// 进口食品属性
      this.commodity_food = null,
      this.commodity_phone = null,
      this.commodity_home = null
    },
    // 选择取消按钮
    selectCancel() {
      this.spopen =false;
      this.Classify = "null";
      this.commodity_food = null,
      this.commodity_phone = null,
      this.commodity_home = null
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        commodityId: null,
        commodityName: null,
        commodityClassify: null,
        commodityBrand: null,
        commodityFlag: null,
        commodityTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commodityId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品列表";
    },
    /** 新增按钮操作 */
    selectAdd() {
      this.reset();
      this.spopen = true;
      this.title = "选择商品分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const commodityId = row.commodityId || this.ids
      getController(commodityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品列表";
      });
    },
    /** 改进后的修改按钮操作 */
    selectUpdate(row) {
      this.reset();
      const commodityId = row.commodityId || this.ids
      getController(commodityId).then(response => {
        if (row.commodityClassify != null) {
          this.form = response.data;
          this.title = "修改商品列表";
          //如果选了值的话,就修改商品分类的属性并且添加到新增值里面
          if (row.commodityClassify== 1) {
            //如果选了值的话,就修改商品分类的属性并且添加到新增值里面
            this.Classify = "进口食品";
            this.spopen = false;
            this.open = true;
            this.commodity_food = 1;
          }else if(row.commodityClassify == 2){
            //如果选了值的话,就修改商品分类的属性并且添加到新增值里面
            this.Classify = "数码产品";
            this.spopen = false;
            this.open = true;
            this.commodity_phone = 1;
          }else if(row.commodityClassify == 3){
            //如果选了值的话,就修改商品分类的属性并且添加到新增值里面
            this.Classify = "生活家电";
            this.spopen = false;
            this.open = true;
            this.commodity_home = 1;
          }
        }
      });
    },
    /** 选择商品分类 */
    selectForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.commodityClassify != null) {
            //如果选了值的话,就修改商品分类的属性并且添加到新增值里面
            if (this.form.commodityClassify == 1) {
              //如果选了值的话,就修改商品分类的属性并且添加到新增值里面
              this.Classify = "进口食品";
              this.spopen = false;
              this.open = true;
              this.commodity_food = 1;
            }else if(this.form.commodityClassify == 2){
              //如果选了值的话,就修改商品分类的属性并且添加到新增值里面
              this.Classify = "数码产品";
              this.spopen = false;
              this.open = true;
              this.commodity_phone = 1;
            }else if(this.form.commodityClassify == 3){
              //如果选了值的话,就修改商品分类的属性并且添加到新增值里面
              this.Classify = "生活家电";
              this.spopen = false;
              this.open = true;
              this.commodity_home = 1;
            }
          } else {}
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.commodityId != null) {
            updateController(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.commodity_food = null,
              this.commodity_phone = null,
              this.commodity_home = null
            });
          } else {
            addController(this.form).then(response => {
              console.log(this.form)
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.commodity_food = null,
              this.commodity_phone = null,
              this.commodity_home = null
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commodityIds = row.commodityId || this.ids;
      this.$modal.confirm('是否确认删除商品列表编号为"' + commodityIds + '"的数据项？').then(function() {
        return delController(commodityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/controller/export', {
        ...this.queryParams
      }, `controller_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
